package com.tangyee.home_exercise_diet.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.tangyee.R;

/**
 * 预算热量说明的Activity，就一个EditText，一个按钮，几个TextView
 */
public class About extends AppCompatActivity {

    private EditText mEditText;
    private Button sure;

    private int budget;

    private ImageView back;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        mEditText = findViewById(R.id.edit);
        sure = findViewById(R.id.sure);
        budget = getIntent().getIntExtra("budget", 0);

        mEditText.setHint(budget + "");
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String s = charSequence.toString();
                if (!s.trim().isEmpty())
                    budget = Integer.parseInt(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                if (budget<1000)
                    budget = 1000;
                if (budget>4000)
                    budget = 4000;
                intent.putExtra("budget", budget);
                setResult(2, intent);

                finish();
            }
        });

        back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
}
